package org.news.web.controller;

import java.util.List;

import com.dtflys.forest.annotation.Get;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.news.web.constant.CategoriesStatus;
import org.news.web.constant.NotificationConstant;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.NotificationsVo;
import org.news.web.domain.bo.NotificationsBo;
import org.news.web.service.INotificationsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 通知功能
 *
 * @author Gu Ci
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/notifications")
public class NotificationsController extends BaseController {

    private final INotificationsService notificationsService;

    /**
     * 查询通知功能列表
     */
    @SaCheckPermission("web:notifications:list")
    @GetMapping("/list")
    public TableDataInfo<NotificationsVo> list(NotificationsBo bo, PageQuery pageQuery) {
        return notificationsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出通知功能列表
     */
    @SaCheckPermission("web:notifications:export")
    @Log(title = "通知功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NotificationsBo bo, HttpServletResponse response) {
        List<NotificationsVo> list = notificationsService.queryList(bo);
        ExcelUtil.exportExcel(list, "通知功能", NotificationsVo.class, response);
    }

    /**
     * 获取通知功能详细信息
     *
     * @param notificationId 主键
     */
    @SaCheckPermission("web:notifications:query")
    @GetMapping("/{notificationId}")
    public R<NotificationsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long notificationId) {
        return R.ok(notificationsService.queryById(notificationId));
    }

    /**
     * 新增通知功能
     */
    @SaCheckPermission("web:notifications:add")
    @Log(title = "通知功能", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NotificationsBo bo) {
        return toAjax(notificationsService.insertByBo(bo));
    }

    /**
     * 修改通知功能
     */
    @SaCheckPermission("web:notifications:edit")
    @Log(title = "通知功能", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NotificationsBo bo) {
        return toAjax(notificationsService.updateByBo(bo));
    }

    /**
     * 删除通知功能
     *
     * @param notificationIds 主键串
     */
    @SaCheckPermission("web:notifications:remove")
    @Log(title = "通知功能", businessType = BusinessType.DELETE)
    @DeleteMapping("/{notificationIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] notificationIds) {
        return toAjax(notificationsService.deleteWithValidByIds(List.of(notificationIds), true));
    }

    /**
     * 获取已读状态列表
     */
    @GetMapping("/getIsReadStatus")
    public R<List<CategoriesStatus>> isReadStatus(){
        return R.ok(NotificationConstant.ReadStatus());
    }
    /**
     * 获取通知状态列表
     */
    @GetMapping("/getNoStatus")
    public R<List<CategoriesStatus>> NoStatus(){
        return R.ok(NotificationConstant.NoStatus());
    }
}
